package main

import (
	"encoding/json"
	"fmt"
	"net"
)

// var serverAddr = "192.168.31.32:9000"
var serverAddr = "127.0.0.1:9000"

func main() {

	var name, sex, age string

	fmt.Println("请输入姓名:")
	fmt.Scan(&name)

	fmt.Println("请输入性别:")
	fmt.Scan(&sex)

	fmt.Println("请输入年龄:")
	fmt.Scan(&age)

	// 发送信息给到服务端
	var sendMap map[string]interface{}
	sendMap = make(map[string]interface{})
	sendMap["name"] = name
	sendMap["sex"] = sex
	sendMap["age"] = age

	sendStr, err := json.Marshal(sendMap)
	if err != nil {
		fmt.Println("编码json失败：", err)
	}

	sendInfo(string(sendStr))

	fmt.Println("发送成功")

}

func sendInfo(sendStr string) {
	tcpAddr, err := net.ResolveTCPAddr("tcp", serverAddr)
	if err != nil {
		fmt.Println("请求服务器失败：", err)
	}

	conn, err := net.DialTCP("tcp4", nil, tcpAddr)
	defer conn.Close()
	if err != nil {
		fmt.Println("连接服务器失败：", err)
	}

	_, err = conn.Write([]byte(sendStr))
	if err != nil {
		fmt.Println("发送信息失败：", err)
	}

	return

}
